package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.TMStatMfdMapper;
import cc.rengu.igas.momp.common.entity.TMStatMfd;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 手续费减免活动统计表数据数据库操作接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/4/20 22:31
 */

public class TMStatMfdMapperImpl implements TMStatMfdMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 使用主键获取手续费减免活动统计表数据数据库操作接口实现
     *
     * @param instId    法人机构编号
     * @param mcNo      营销活动编号
     * @param ruleIndex 规则顺序号
     * @return cc.rengu.igas.momp.common.entity.TMStatMfd 手续费减免活动统计表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/20 22:40
     */
    @Override
    public TMStatMfd selectMchntFeeDiscountStatisticsByPK(String instId, String mcNo, String ruleIndex) throws Exception {

        TMStatMfd tmStatMfd = new TMStatMfd();
        tmStatMfd.setInstId(instId);
        tmStatMfd.setMcNo(mcNo);
        tmStatMfd.setRuleIndex(ruleIndex);

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntFeeDiscountStatisticsByPK");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatMfd, tmStatMfd);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>的商户手续费优惠活动<{}>规则编号<{}>的统计数据失败!<{}>",
                    instId, mcNo, ruleIndex, iReturnCode);
            return null;
        }

        return tmStatMfd;
    }
}
